﻿using Goodsmoels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json;

namespace GoodsDALs
{
  public   class Goodsdal
    {


        //依赖注入
        private readonly GoodDbContext _goodDbContext;
        private readonly IDistributedCache _catha;
        public Goodsdal (GoodDbContext goodDbContext, IDistributedCache catha)
        {
            _goodDbContext = goodDbContext;
            _catha = catha;
        }




        //注册
        public int UsersAdd(Users u)
        {
            _goodDbContext.Users.Add(u);
            return _goodDbContext.SaveChanges();
        }

        //登录
        public Users DenggLu(string UsersName,string UsersPwd)
        {
            return _goodDbContext.Users.Where(a => a.UsersName == UsersName&&a.UsersPwd==UsersPwd).FirstOrDefault();
        }


        //商品显示
        public List<ViewShow>GoodsShow()
        {
            var sql = from a in _goodDbContext.Goods
                      select new ViewShow
                      {
                          GoodsId = a.GoodsId,
                          GoodsImages = "http://localhost:31437/StaticFiles/" + a.GoodsImages,
                          GoodsIntroduce = a.GoodsIntroduce,
                          GoodsName = a.GoodsName,
                          GoodsPrice = a.GoodsPrice
                      };

            return sql.ToList();
        }

     //  购物车添加
       public int AddShop(Shops s)
        {
            List<Shops> list = new List<Shops>();
            var Gcar = _catha.GetString("Gcar");
            if(string.IsNullOrWhiteSpace(Gcar))
            {
                list.Add(s);
                _catha.SetString("Gcar",JsonConvert.SerializeObject(list)); 
            }
            else
            {
                list = JsonConvert.DeserializeObject<List<Shops>>(Gcar);
                var item = list.Where(a => a.ShopsId == s.ShopsId).FirstOrDefault();
                item.ShopNum += s.ShopNum;
                _catha.SetString("Gcar", JsonConvert.SerializeObject(list));
            }
            _goodDbContext.Shops.Add(s);
            return _goodDbContext.SaveChanges();
         
        }
        
        //购物车显示
        public List<ShopView>ShopShow()
        {
            var sql = from a in _goodDbContext.Shops
                      join b in _goodDbContext.Goods on a.GoodsId equals b.GoodsId
                      join c in _goodDbContext.Users on a.UseresId equals c.UsersId
                      select new ShopView
                      {
                          GoodsId = a.GoodsId,
                          GoodsName = b.GoodsName,
                          GoodsImages = "http://localhost:31437/StaticFiles/" + b.GoodsImages,
                          GoodsPrice = b.GoodsPrice,
                           GoodsIntroduce=b.GoodsIntroduce,
                          ShopsId = a.ShopsId,
                          ShopNum = a.ShopNum

                      };
            return sql.ToList();

        }


        //商品详情
        public Goods GoodsFan(int GoodsId)
        {
            return _goodDbContext.Goods.Find(GoodsId);
         
        }

        //地址添加
        public int AddAddr(Addresss a)
        {
            _goodDbContext.Addresss.Add(a);
            return _goodDbContext.SaveChanges();
        }

        //购物车修改
        public int UpdateShop(int ShopsId,int ShopNum)
        {
            var q = _goodDbContext.Shops.Find(ShopsId);
            q.ShopNum = ShopNum;
            return _goodDbContext.SaveChanges();
        }

        //地址显示
        public List<ViewAddres>AddreShow()
        {
            var sql = from a in _goodDbContext.Addresss
                      join b in _goodDbContext.City on a.CityId equals b.CityId
                      join c in _goodDbContext.City on a.AddresShi equals c.CityId
                      join d in _goodDbContext.City on a.AddresXian equals d.CityId
                      select new ViewAddres
                      {
                          AddresId = a.AddresId,
                          AddresConsignee = a.AddresConsignee,
                          CityId = a.CityId,
                          ShengName = b.CityName,
                          AddresShi = a.AddresShi,
                          ShiName = c.CityName,
                          AddresXian
                      = a.AddresXian,
                          XianName = d.CityName,
                           AddresPhone=a.AddresPhone  

                      };
            return sql.ToList();
        }

        //联动
        public List<City>CityBing(int Pid)
        {
            return _goodDbContext.City.Where(a => a.Pid == Pid).ToList();
        }


        //删除购物车
        public int ShopDel(int ShopsId)
        {
            var q = _goodDbContext.Shops.Find(ShopsId);
            _goodDbContext.Shops.Remove(q);
            return _goodDbContext.SaveChanges();

        }


        //删除地址
        public int DelAddre(int AddresId)
        {
            var q = _goodDbContext.Addresss.Find(AddresId);
            _goodDbContext.Addresss.Remove(q);
            return _goodDbContext.SaveChanges();
        }
           
        //编辑地址
        public Addresss AddreFan(int AddresId)
        {
            return _goodDbContext.Addresss.Find(AddresId);
        }
        
        //修改地址
        public  int UpdateAddre(Addresss a)
        {
            _goodDbContext.Entry(a).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return _goodDbContext.SaveChanges();
        }
        
      

    }
}
